package com.fifth.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fifth.business.mapper.AppealMapper;
import com.fifth.business.service.AppealService;
import com.fifth.common.pojo.dto.CommonResult;
import com.fifth.common.pojo.entity.Appeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class AppealServiceImpl implements AppealService {

    @Autowired
    private AppealMapper appealMapper;

    /**
     * 申诉查询
     * @param appeal
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public CommonResult appealQuery(Appeal appeal, Integer currentPage, Integer pageSize) {
        System.out.println(appeal.toString());
        IPage<Appeal> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Appeal> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(appeal.getBusinessId()),"business_id",appeal.getBusinessId());
        if (appeal.getBusinessId() == null || appeal.getBusinessId() == ""){
            wrapper = null;
        }
        IPage<Appeal> appealIPage=appealMapper.selectAll(page,wrapper);
        System.out.println("总页码:" + appealIPage.getPages());
        System.out.println("总条数:" + appealIPage.getTotal());
        System.out.println("当前页记录:" + appealIPage.getRecords());
        Map<String,Object> map = new HashMap<>();
        map.put("totel",appealIPage.getTotal());
        map.put("pages",appealIPage.getPages());
        map.put("records",appealIPage.getRecords());
        return new CommonResult(2000, "查询成功", map);



    }

    /**
     * 通过
     * @param id
     * @param status
     * @return
     */
    @Override
    public CommonResult tongguo(String id, Integer status) {
       Appeal appeal = new Appeal();
       appeal.setStatus(status);
       QueryWrapper<Appeal> queryWrapper = new QueryWrapper<>();
       queryWrapper.eq("id",id);
        int update = appealMapper.update(appeal, queryWrapper);

        return new CommonResult(2000,"审核通过",update);
    }

    /**
     * 驳回
     * @param status
     * @return
     */
    @Override
    public CommonResult bohui(String id,Integer status) {
    Appeal appeal = new Appeal();
    appeal.setStatus(status);
        QueryWrapper<Appeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        int update = appealMapper.update(appeal, queryWrapper);
        return new CommonResult(2000,"审核通过",update);



    }

    /**
     * 商家申诉
     * @param appeal
     * @return
     */
    @Override
    public CommonResult shangjiashensu(Appeal appeal) {
        System.out.println(appeal);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前时间String类型
        String time_string = sdf.format(date);
        appeal.setAppealTime(time_string);
        appeal.setStatus(0);
        int insert = appealMapper.insert(appeal);
        return new CommonResult(2000,"审核通过",insert);
    }


}
